package com.kizitonwose.calendarview;

import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.app.Context;

/**
 * @author LiuQingHua
 * @version 1.0
 * @date 2021/2/26 9:43
 */
public class EasyDialog {
    private CommonDialog dialog;
    private Context context;
    private Component parse;

    private EasyDialog(Context context) {
        this.context = context;
        this.dialog = new CommonDialog(context);
    }

    public static EasyDialog getInstance(Context context) {
        return new EasyDialog(context);
    }

    class MyCommonDialog extends CommonDialog {

        public MyCommonDialog(Context context) {
            super(context);
        }

        @Override
        protected void onCreate() {
            super.onCreate();
        }
    }

    public EasyDialog setContentResId(int resId) {
        parse = LayoutScatter.getInstance(context).parse(resId, null, false);
        dialog.setContentCustomComponent(parse);
        return this;
    }

    public EasyDialog setText(int id, String str) {
        ((Text) findComponentById(id)).setText(str);
        return this;
    }

    public Text getText(int id) {
        return findComponentById(id);
    }

    public TextField getTextField(int id) {
        return findComponentById(id);
    }


    public EasyDialog setClickListener(int id, ClickedListener clickedListener) {
        findComponentById(id).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                clickedListener.onClick(EasyDialog.this);
            }
        });
        return this;
    }

    public EasyDialog show() {
        dialog.show();
        return this;
    }

    public EasyDialog setTitleText(String text) {
        dialog.setTitleText(text);
        return this;
    }

    public EasyDialog setSize(int width, int height) {
        dialog.setSize(width, height);
        return this;
    }

    public EasyDialog destroy() {
        dialog.destroy();
        return this;
    }

    public EasyDialog registerDisplayListener(DisplayListener listener) {
        dialog.registerDisplayCallback(new BaseDialog.DisplayCallback() {
            @Override
            public void onDisplay(IDialog iDialog) {
                listener.onDisplay(EasyDialog.this);
            }
        });
        return this;
    }


    private <T extends Component> T findComponentById(int id) {
        return (T) parse.findComponentById(id);
    }

    public CommonDialog getDialog() {
        return dialog;
    }

    public interface ClickedListener {
        void onClick(EasyDialog dialog);
    }

    public interface DisplayListener {
        void onDisplay(EasyDialog dialog);
    }
}
